package com.yjk.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yjk.mall.product.entity.AttrEntity;
import com.yjk.mall.product.service.AttrAttrgroupRelationService;
import com.yjk.mall.product.service.AttrService;
import com.yjk.mall.product.vo.AttrGroupResVo;
import com.yjk.mall.product.vo.AttrRelDelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yjk.mall.product.entity.AttrGroupEntity;
import com.yjk.mall.product.service.AttrGroupService;
import com.yjk.common.utils.PageUtils;
import com.yjk.common.utils.R;



/**
 * 属性分组
 *
 * @author yjk
 * @email jk.ye@qq.com
 * @date 2020-06-10 12:16:20
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    AttrGroupService attrGroupService;

    @Autowired
    AttrService attrService;

    @Autowired
    AttrAttrgroupRelationService relationService;
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("mall-product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
        //PageUtils page = attrGroupService.queryPage(params);

        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }

    @GetMapping("/{catelogId}/withattr")
    public R withAttr(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupResVo> vos =attrGroupService.withAttr(catelogId);
        return R.ok().put("data",vos);
    }

    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody List<AttrRelDelVo>  vos){
        attrService.deleteBatch(vos);
        return R.ok();
    }

    @PostMapping("/attr/relation")
    public R saveRelation(@RequestBody List<AttrRelDelVo>  vos){
            relationService.saveRelation(vos);
        return R.ok();
    }

    @GetMapping("{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId){
        List<AttrEntity> list=attrGroupService.queryRelation(attrGroupId);
        return R.ok().put("data", list);

    }

    @GetMapping("{attrGroupId}/noattr/relation")
    public R noAttrRelation(@RequestParam Map<String, Object> params,@PathVariable("attrGroupId") Long attrGroupId){
        PageUtils page=attrGroupService.queryNoRelation(params,attrGroupId);
        return R.ok().put("page", page);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("mall-product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("mall-product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("mall-product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("mall-product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
